﻿<UserControl x:Class="SuperQuery.UserControls.QueryTab"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:sqcon="clr-namespace:SuperQuery.Converters"
             xmlns:tb="clr-namespace:SuperQuery.UserControls.ToolBars"
             mc:Ignorable="d" 
             Loaded="UserControl_Loaded"
             d:DesignHeight="403" d:DesignWidth="548" TextOptions.TextFormattingMode="Display">
    <UserControl.Resources>
        <sqcon:QueryTypeToStringConverter x:Key="QueryTypeToString"/>
        <sqcon:QueryResultToDataTableConverter x:Key="QueryResultToDataTable"/>
        <sqcon:QueryResultToStringConverter x:Key="QueryResultToString"/>
        <sqcon:InverseBoolConverter x:Key="InverseBool"/>
        <sqcon:QueryResultToIconConverter x:Key="QueryResultToIcon"/>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        
        <!--Template for dislaying the server name and database name in the list of databases to run the query against-->
        <DataTemplate x:Key="DatabaseCell">
            <StackPanel Orientation="Horizontal">
                <Button x:Name="btnEditDB" Tag="{Binding Path=DatabaseId}" Click="btnEditDB_Click" MinWidth="0">
                    Edit...
                </Button>
                <TextBlock Text="{Binding Path=DatabaseServer.ServerName}" Margin="0,8,0,0"/>
                <TextBlock Text=" - " Margin="0,8,0,0"/>
                <TextBlock Text="{Binding Path=DatabaseName}" Margin="0,8,0,0"/>
            </StackPanel>
        </DataTemplate>

        <!--Template for the Selected database cell-->
        <DataTemplate x:Key="DatabaseSelectCell">
            <CheckBox HorizontalContentAlignment="Center"
                      HorizontalAlignment="Center"
                      VerticalContentAlignment="Center"
                      VerticalAlignment="Center"
                      IsChecked="{Binding Path=Selected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </DataTemplate>

        <!--Template for the image cell that indicates the status of the database-->
        <DataTemplate x:Key="DatabaseImageCell">
            <Image Source="{Binding Path=Result, Converter={StaticResource QueryResultToIcon}}" MaxHeight="16" MaxWidth="16"/>
        </DataTemplate>

        <!--Template for the Database Type drop down (list of plug ins) in the Database Filter-->
        <DataTemplate x:Key="PluginItem">
            <TextBlock Text="{Binding Value}"/>
        </DataTemplate>
    </UserControl.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!--Tool bar for this query-->
        <tb:QueryToolBar  Grid.Row="0" Grid.Column="2" x:Name="dbQueryToolBar" 
                          Enabled="{Binding EditorEnabled}"
                          ToolBarOperation="dbQueryToolBar_ToolBarOperation" />

        <!--Text box to enter the query-->
        <TextBox Grid.Row="1" Grid.Column="2" Grid.RowSpan="2"
                       HorizontalScrollBarVisibility="Auto"
                       VerticalScrollBarVisibility="Auto"
                       AcceptsReturn="True"
                       x:Name="txtQuery"
                       FontFamily="Courier New"
                       Text="{Binding Path=LoadedQuery.QueryText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                       IsReadOnly="{Binding Path=EditorEnabled, Converter={StaticResource InverseBool}}" TextChanged="txtQuery_TextChanged"/>
        
        <GridSplitter Grid.Row="2"
                      Grid.Column="3"
                      Height="5"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Top"
                      ResizeBehavior="PreviousAndNext"/>

        <!--Tab control containng the data grid and and any text information from the results of the query-->
        <TabControl Grid.Row="3" Grid.Column="3">
            <TabItem Header="Results">
                <DataGrid Name="grdData" CanUserReorderColumns="False" ClipboardCopyMode="IncludeHeader" IsReadOnly="True" 
                          ItemsSource="{Binding Path=SelectedResult, Converter={StaticResource QueryResultToDataTable}}"/>
            </TabItem>
            <TabItem Header="Output" >
                <TextBox IsReadOnly="True" VerticalScrollBarVisibility="Visible" TextWrapping="Wrap" Name="txtOutput"
                         Text="{Binding Path=SelectedResult, Converter={StaticResource QueryResultToString}}"/>
            </TabItem>
        </TabControl>

        <GridSplitter 
                    Grid.Column="1"
                    Grid.Row="1"
                    Grid.RowSpan="3"
                    Width="5"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Stretch"
                    ResizeBehavior="PreviousAndNext"/>

        <!--Database Grid List Controls-->
        <DockPanel Grid.Row="0" Grid.Column="0" Grid.RowSpan="4">

            <!--Database list ToolBar-->
            <tb:DatabaseListToolBar x:Name="dbListToolBar" Grid.Row="0" DockPanel.Dock="Top"
                                Enabled="{Binding EditorEnabled}" Height="30"
                                ToolBarOperation="dbListToolBar_ToolBarOperation" />

            <!--Database Grid Filter-->
            <Expander IsExpanded="False" Grid.Row="1" DockPanel.Dock="Top">
                <Expander.Header>
                    <StackPanel Orientation="Horizontal">
                        <Label>Database Filter</Label>
                        <Label FontWeight="Bold" Visibility="{Binding Path=FilterApplied, Converter={StaticResource BooleanToVisibilityConverter}}">(Active)</Label>
                    </StackPanel>
                </Expander.Header>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="90"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <!--Database name filter-->
                    <TextBlock Grid.Row="0" Grid.Column="0" Margin="5,7,5,5">Database:</TextBlock>
                    <TextBox Grid.Row="0" Grid.Column="1" Margin="5" Text="{Binding DatabaseNameFilter}"/>

                    <!--Database plugin type filter-->
                    <TextBlock Grid.Row="1" Grid.Column="0" Margin="5,7,5,5">Database Type:</TextBlock>
                    <ComboBox Grid.Row="1" Grid.Column="1" Margin="5" x:Name="cboPlugins"
                              ItemTemplate="{StaticResource PluginItem}" ItemsSource="{Binding AvailablePlugins}"/>

                    <!--Databases that have had successfull query runs-->
                    <CheckBox Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="5" IsChecked="{Binding SuccessfullQueryFilter}">
                        Successfull Queries
                    </CheckBox>

                    <!--Buttons to Apply or Remove the filter-->
                    <StackPanel Orientation="Horizontal" Grid.Row="4" Grid.ColumnSpan="2">
                        <Button x:Name="btnApplyFilter" Style="{StaticResource iconButton}"
                                Click="btnApplyFilter_Click" ToolTip="Apply Filter">
                            <Image Height="16" Source="..\Icons\Apply.ico"/>
                        </Button>
                        <Button x:Name="btnRemoveFilter" Style="{StaticResource iconButton}"
                                Margin="0,5,0,5" Click="btnRemoveFilter_Click" ToolTip="Remove Filter">
                            <Image Height="16" Source="..\Icons\Remove.ico"/>
                        </Button>
                    </StackPanel>
                </Grid>
            </Expander>

            <!--Grid to hold the list of databases we want to run queries against-->
            <DataGrid x:Name="grdDatabases" Grid.Row="2"
                  AutoGenerateColumns="False" ItemsSource="{Binding DisplayedDatabases}" 
                  SelectionMode="Single"  IsEnabled="{Binding Path=EditorEnabled}"
                  SelectedCellsChanged="grdDatabases_SelectedCellsChanged" KeyDown="grdDatabases_KeyDown">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Database" Width="5*" CellTemplate="{StaticResource DatabaseCell}"/>
                    <DataGridTemplateColumn Header="Selected" Width="1.5*" CellTemplate="{StaticResource DatabaseSelectCell}"/>
                    <DataGridTemplateColumn Header="Status" Width="1.2*" CellTemplate="{StaticResource DatabaseImageCell}"/>
                </DataGrid.Columns>
            </DataGrid>
        </DockPanel>

        <!--Status Bar-->
        <StatusBar Grid.Row="4" Grid.ColumnSpan="3" >
            <TextBlock Text="{Binding Status}"/>
            <StatusBarItem HorizontalContentAlignment="Stretch">
                <ProgressBar Value="{Binding Progress}" Height="15">
                    <ProgressBar.Style>
                        <Style TargetType="{x:Type ProgressBar}">
                            <Style.Triggers>
                                <!--Hide progress bar when progress value is 0-->
                                <DataTrigger Binding="{Binding Progress}" Value="0">
                                    <Setter Property="Visibility" Value="Hidden"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ProgressBar.Style>
                </ProgressBar>
            </StatusBarItem>
        </StatusBar>
    </Grid>

</UserControl>
